import { Application, codo } from "egg";
import WeWorkBaseService from "./WeWorkBaseService";
import CodoError from "../CodoError";
import { WeWork } from "./type";

export default class Checkin extends WeWorkBaseService {
	constructor(
		protected config: codo.WeWork.IWeWorkConfig,
		private accessToken: string,
		protected app: Application
	) {
		super(config, app);
	}

	/**
	 *查询打卡记录
	 *
	 * @param {Date} startTime 开始时间
	 * @param {Date} endTime 结束时间
	 * @param {string[]} useridList 用户id列表
	 * @return {*}
	 * @memberof PlatformQiyeWeixinCheckinService
	 */
	async getCheckinRecords(startTime: Date, endTime: Date, useridList: string[]) {
		const starttime = Math.ceil(startTime.getTime() / 1000);
		const endtime = Math.floor(endTime.getTime() / 1000);
		const sendData: WeWork.IRequestCheckinGetData = {
			opencheckindatatype: 3,
			starttime,
			endtime,
			useridlist: useridList,
		};

		this.app.logger.debug("[企业微信 查询打卡记录] 参数 %j", sendData);

		const url = `https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=${this.accessToken}`;
		const res = await this.curl<WeWork.IResponseCheckinGetData>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: sendData,
		});

		// this.app.logger.debug("[] %j", res);

		if (res.data.errcode === 0) {
			return res.data.checkindata;
		}
		this.app.logger.error("[企业微信 查询打卡记录] 错误 %s", res.data.errmsg);

		throw new CodoError(res.data.errmsg);
	}

	/**
	 *查询打卡日报信息
	 * - https://developer.work.weixin.qq.com/document/path/93374
	 *
	 * @param {Date} startTime
	 * @param {Date} endTime
	 * @param {string[]} useridList
	 * @return {*}
	 * @memberof PlatformQiyeWeixinCheckinService
	 */
	async getDayReports(startTime: Date, endTime: Date, useridList: string[]) {
		const starttime = Math.ceil(startTime.getTime() / 1000);
		const endtime = Math.floor(endTime.getTime() / 1000);
		const sendData: WeWork.IRequestCheckinDayReports = {
			starttime,
			endtime,
			useridlist: useridList,
		};

		const url = `https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckin_daydata?access_token=${this.accessToken}`;
		const res = await this.curl<WeWork.IResponseCheckinDayReports>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: sendData,
		});

		if (res.data.errcode === 0) {
			return res.data.checkindata;
		}
		this.app.logger.error("[企业微信 查询打卡日报] 错误 %s", res.data.errmsg);

		throw new CodoError(res.data.errmsg);
	}

	/**
	 *
	 * 查询打卡设备的原始刷卡记录 ，含未被打卡应用记录的数据
	 * - https://developer.work.weixin.qq.com/document/path/94126
	 *
	 * @param {Date} startTime
	 * @param {Date} endTime
	 * @param {string[]} useridList
	 * @memberof PlatformQiyeWeixinCheckinService
	 */
	async getHardwareCheckinData(startTime: Date, endTime: Date, useridList: string[]) {
		const url = `https://qyapi.weixin.qq.com/cgi-bin/hardware/get_hardware_checkin_data?access_token=${this.accessToken}`;
		const starttime = Math.ceil(startTime.getTime() / 1000);
		const endtime = Math.floor(endTime.getTime() / 1000);
		const sendData = {
			filter_type: 1,
			starttime,
			endtime,
			useridlist: useridList,
		};

		const res = await this.curl<WeWork.IResponseDeviceCheckinData>(url, {
			method: "POST",
			contentType: "json",
			dataType: "json",
			data: sendData,
		});

		if (res.data.errcode === 0) {
			return res.data.checkindata;
		}
		this.app.logger.error("[企业微信 查询设备打卡数据] 错误 %s", res.data.errmsg);

		throw new CodoError(res.data.errmsg);
	}
}
